﻿/*
 * File: GamePlayBaseScene_Helper.cs
 * Author: Gourav Das
 * Purpose: Base Scene and helpers for gameplay
 * Created: Jun17 2011
 */

using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using System.IO;
using _22yards._2D;
using _22yards.Scenes.Helpers;
using _22yards.Core;
using _22yards;
using _22yards.EnumDeclarations;
using _22yards.ThreeD;
using _22yards.Helpers;
using Komires.MataliPhysics;

namespace _22yards.Scenes
{
	/// <summary>
	/// This is a scene that implements the Base Gameplay scene
	/// </summary>
	partial class GamePlayBaseScene : Scene
	{
		#region General Base Gameplay Scene

		private Vector3 SunlightDir;
		private bool ShowBoundingBoxes = false;
		private bool ShowPhysicsObjects = false;
		private bool ShowModels = true;
		private bool ShowBloom = false;
		private bool ShowShadowMap = false;
		private bool ShowGaussianBlur = false;
		private bool ShowShadows = false;
		private bool ReliefMapping = false;

		private bool PausePhysics = false;

		private short eGamePlayType;
		private ContentManager content;

		//Declaration of HLSL Effects
		private Effect simpleEff;
		private Effect ColorNormalGlowEff;
		private Effect PostEffect;

		// Render target
		RenderTarget2D RenderTargetBloom;
		RenderTarget2D RenderTargetBlurBloom;
		RenderTarget2D RenderTargetBlurIIBloom;
		Texture2D BloomTexture;
		Texture2D BlurBloomTexture;
		Texture2D BlurIIBloomTexture;


		float[] weights = null;
		Vector2[] offsetsHoriz = null;
		Vector2[] offsersVert = null;

		#endregion

		private void FillGaussianBlurVariables(ref float[] weights, ref Vector2[] offsetsHoriz, ref Vector2[] offsetsVert)
		{
			int GaussianBlurRadius = 3;//If u change this, change it in effects file
			float amount = 0.001f;

			weights = null;
			weights = new float[GaussianBlurRadius * 2 + 1];
			float sigma = GaussianBlurRadius / amount;

			float twoSigmaSquare = 2.0f * sigma * sigma;
			float sigmaRoot = (float)Math.Sqrt(twoSigmaSquare * Math.PI);
			float total = 0.0f;
			float distance = 0.0f;
			int index = 0;

			for (int i = -GaussianBlurRadius; i <= GaussianBlurRadius; ++i)
			{
				distance = i * i;
				index = i + GaussianBlurRadius;
				weights[index] = (float)Math.Exp(-distance / twoSigmaSquare) / sigmaRoot;
				total += weights[index];
			}

			for (int i = 0; i < weights.Length; ++i)
				weights[i] /= total;

			offsetsHoriz = null;
			offsetsHoriz = new Vector2[GaussianBlurRadius * 2 + 1];

			offsetsVert = null;
			offsetsVert = new Vector2[GaussianBlurRadius * 2 + 1];

			index = 0;
			float xOffset = 1.0f / sceneEngine.GraphicsDevice.DisplayMode.Width;
			float yOffset = 1.0f / sceneEngine.GraphicsDevice.DisplayMode.Height;

			for (int i = -GaussianBlurRadius; i <= GaussianBlurRadius; ++i)
			{
				index = i + GaussianBlurRadius;
				offsetsHoriz[index] = new Vector2(i * xOffset, 0.0f);
				offsetsVert[index] = new Vector2(0.0f, i * yOffset);
			}

		}
	}
}